package main

import (
	"flag"
	"log"
	"my_go_open/utils"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	url := ""
	flag.StringVar(&url, `url`, "amqp://root:abc123@localhost:5672/", `mq的连接参数`)
	flag.Parse()

	if url == "" {
		log.Fatalln(`请输入MQ的连接参数`)
	}
	qName := `TestQ`

	ac, err := utils.NewAmqpClient(url)
	if err != nil {
		log.Fatalln(err)
	}
	ch, err := ac.GetChannel()
	if err != nil {
		log.Fatalln(err)
	}
	_, err = ch.QueueDeclare(qName, false, false, false, false, nil)
	if err != nil {
		log.Fatalln(err)
	}

	ms, err := ch.Consume(qName, `test_consumer`, false, false, false, false, nil)
	if err != nil {
		log.Fatalln(err)
	}
	go func() {
		for {
			select {
			case m := <-ms:
				utils.VarDump(string(m.Body))
				if err := m.Ack(false); err != nil {
					utils.VarDump(err.Error())
				}
			}
		}
	}()

	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
}
